package com.example.demo1122.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo1122.domain.ResponseResult;
import com.example.demo1122.entity.Classstu;
import com.example.demo1122.mapper.ClassstuMapper;
import com.example.demo1122.service.IClassstuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2023-12-19
 */
@Service
public class ClassstuServiceImpl extends ServiceImpl<ClassstuMapper, Classstu> implements IClassstuService {
    @Autowired
    private ClassstuMapper classStuMapper;
//新增学生与课堂联系
    @Override
    public ResponseResult addStudentToClass(String studentID, Integer classID) {
        int count = classStuMapper.countByClassIdAndStudentId(classID, studentID);
        if (count == 0) {
            // 如果不存在，执行插入操作
            Classstu classStu = new Classstu();
            classStu.setStudentID(studentID);
            classStu.setClassID(classID);
            classStuMapper.insert(classStu);
        }
        return new ResponseResult(200, "Student added to class successfully");
    }
    //删除学生与课堂联系
    @Override
    public boolean deleteStudentFromClass(Integer classID, String studentID) {
        // 调用mapper的删除方法
        int result = classStuMapper.deleteByClassIdAndStudentId(classID, studentID);
        return result > 0;
    }
    //批量删除学生与课堂联系
    @Override
    public boolean deleteStudentClasses(List<Classstu> keys) {
        for (Classstu key : keys) {
            // 调用mapper或repository来删除每个关联
            // 例如，你可以使用MyBatis的@DeleteProvider或者类似的方法
            classStuMapper.deleteByClassIdAndStudentId(key.getClassID(), key.getStudentID());
        }
        return true;
    }
//查找课堂是否存在
    @Override
    public boolean existsClassId(Integer classID) {
        // 调用mapper或repository中的方法来检查classID
        return classStuMapper.countByClassId(classID) > 0;
    }

}
